#!/usr/bin/env python
#coding=utf-8
import base
from tornado.web import authenticated


@base.route('/')
class IndexHandler(base.BaseHandler):
    def get(self, template_variables={}):
        self.set_header("Content-Type", "text/html; charset=utf-8")
        self.redirect('/login', permanent=False)


@base.route('/login')
class LoginHandler(base.BaseHandler):
    def get(self, template_variables={}):
        self.set_header("Content-Type", "text/html; charset=utf-8")
        self.render("login.html")

    def post(self, *args, **kwargs):
        self.set_header("Content-Type", "text/html; charset=utf-8")
        next = self.get_argument("next", "/")
        username = self.get_argument("username")
        password = self.get_argument("password")
        password = self.md5hash(password)
        if not username or not password:
            self.render("login.html", **{"username": username, "msg": u"登录失败，不允许空数据"})
        else:
            # if self.db.login(username, password):
            #     self.set_secure_cookie("username", username, expires_days=1)
            #     self.redirect(next, permanent=False)
            # else:
            self.redirect("/login", permanent=False)
